package com.commit.controller;

import com.commit.domain.Question;
import com.commit.domain.User;
import com.commit.service.QuestionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @author:zyh
 * @Time:2021-02-27-16:29
 * @email:1269231889@qq.com
 */
@Controller
public class IndexController {
    @Autowired
    private QuestionService questionService;
    @GetMapping("/index")
    public String index(String title, String page, Model model){
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        model.addAttribute("user",principal);
        int currentPage=1;
        int pageCount=2;
        if(title==null) title="";
        //从第二页开始都是通过前台传递的数据
        if(page!=null) currentPage=Integer.valueOf(page);
        List<Question> all = questionService.findAll(currentPage, pageCount, title);
        Integer total = questionService.getTotal(pageCount, title);
        model.addAttribute("questionList",all);
        model.addAttribute("totalPage",total);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("title",title);
        return "index";
    }

}
